﻿using System;
using System.Web;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfCAddriFrame : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                if (!string.IsNullOrEmpty(this.GetFormParam("ID")))
                {
                    doedit(GetFormParam("ID"));
                }
            }

        }
        private void BindControls()
        {
            if (!IsPostBack)
            { 
                BoundFromProvince();
            }
        }


        private void BoundFromProvince()
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_PROVINCE ";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplPROVINCE_ID", ds, "PROVINCE_NME", "PROVINCE_ID", "", "", true); 
            }
        }

        private void BoundFromCity(string strFatherId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_PROVINCE_CITY WHERE (FATHER_ID='" + strFatherId + "')";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplCITY_ID", ds, "CITY_NME", "CITY_ID", "", "", true); 
            }
        }

        private void BoundArea(string strFatherId)
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_PROVINCE_CITY_AREA WHERE (FATHER_ID='" + strFatherId + "')";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplAREA_ID", ds, "AREA_NME", "AREA_ID", "", "", true); 
            }
        }
         

        private void doedit(string strid)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpCustomerReceipt odataobj = new AppLogic.DataAlc.CBizShpCustomerReceipt())
            {
                DataSet ds = odataobj.SelectById(strid);
                if(ds.Tables[0].Rows.Count>0)
                {
                    BoundFromProvince();
                    BoundFromCity(ds.Tables[0].Rows[0]["PROVINCE_ID"].ToString());
                    BoundArea(ds.Tables[0].Rows[0]["CITY_ID"].ToString());


                    this.AddPageRetRow("J_FormBox", ds.Tables[0].Rows[0]);

                    this.SetContrlValue("dplPROVINCE_ID", ds.Tables[0].Rows[0]["PROVINCE_ID"].ToString());
                    this.SetContrlValue("dplCITY_ID", ds.Tables[0].Rows[0]["CITY_ID"].ToString());
                    this.SetContrlValue("dplAREA_ID", ds.Tables[0].Rows[0]["AREA_ID"].ToString());
                }
            }
        } 

        private void doSave()
        { 
            {
                using (msBsc.AppLogic.DataAlc.CBizShpCustomerReceipt odataobj = new AppLogic.DataAlc.CBizShpCustomerReceipt())
                {
                    DataSet odata;
                    DataRow drnew;
                    if (GetPostHtValue("RECE_ADDRESS_ID") == string.Empty)
                    {
                        odata = new Xsd.DataTds.CDmShpCustomerReceipt();
                        drnew = odata.Tables[0].NewRow();
                        drnew["RECE_ADDRESS_ID"] = odataobj.DataObj.NewID();
                        drnew["USER_NME"] = this.oUserInfo.Operator;
                        odata.Tables[0].Rows.Add(drnew);
                    }
                    else
                    {
                        odata = odataobj.SelectById(GetPostHtValue("RECE_ADDRESS_ID"));
                        if (odata.Tables[0].Rows.Count <= 0)
                        {
                            odata = new Xsd.DataTds.CDmShpCustomerReceipt();
                            drnew = odata.Tables[0].NewRow();
                            drnew["RECE_ADDRESS_ID"] = odataobj.DataObj.NewID();
                            drnew["USER_NME"] = this.oUserInfo.Operator;
                            odata.Tables[0].Rows.Add(drnew);
                        }
                        else
                        {
                            drnew = odata.Tables[0].Rows[0];
                        }
                    }

                    msBsc.AppLogic.Cfg.HttpTools.CopyHashToDataRow(this.htPostDataRow, drnew);

                    if (drnew["PROVINCE_ID"].ToString() == string.Empty || drnew["CITY_ID"].ToString() == string.Empty || drnew["AREA_ID"].ToString() == string.Empty)
                    {
                        Alert("请选择所在地区！");
                        return;
                    }
                    if (drnew["STREET_ADDRESS"].ToString() == string.Empty || drnew["STREET_ADDRESS"].ToString().Length < 5 || drnew["STREET_ADDRESS"].ToString().Length>120)
                    {
                        Alert("请填写详细地址，不需要重复填写省市区，必须大于5个字符，小于120个字符！");
                        return;
                    }
                    if (drnew["ZIP_CODE"].ToString() == string.Empty || drnew["ZIP_CODE"].ToString().Length != 6)
                    {
                        Alert("请填写邮政编码， 必须6个字符！");
                        return;
                    }
                    if (drnew["RECEIPT_NME"].ToString() == string.Empty || drnew["RECEIPT_NME"].ToString().Length < 2 || drnew["RECEIPT_NME"].ToString().Length > 20)
                    {
                        Alert("请填写收货人，收货人姓名姓名应为2-20个字符，一个汉字为两个字符！");
                        return;
                    }
                    if (drnew["PHONE_NO"].ToString() == string.Empty && drnew["LANDLINE_NO"].ToString() == string.Empty)
                    {
                        Alert("电话号码、手机号选填一项！");
                        return;
                    }
                    if (drnew["LANDLINE_NO"].ToString() != string.Empty && drnew["AREA_CODE"].ToString() == string.Empty)
                    {
                        Alert("请填写电话号码区号！");
                        return;
                    }
                     
                    odataobj.Update(odata);

                    if (drnew["IS_DEFAULT"].ToString() == "True")
                    {
                        odataobj.DataObj.ExecuteBySQL("UPDATE SHP_CUSTOMER_RECEIPT SET IS_DEFAULT=1 WHERE RECE_ADDRESS_ID='" + drnew["RECE_ADDRESS_ID"].ToString() + "'");
                        odataobj.DataObj.ExecuteBySQL("UPDATE SHP_CUSTOMER_RECEIPT SET IS_DEFAULT=0 WHERE RECE_ADDRESS_ID<>'" + drnew["RECE_ADDRESS_ID"].ToString() + "'");
                    }

                    this.ExecJScript("if(parent &&parent.updateaddress){parent.updateaddress('" + drnew["RECE_ADDRESS_ID"].ToString() + "');} if(parent &&parent.layer){var index = parent.layer.getFrameIndex(window.name);setTimeout(function(){parent.layer.close(index);},200);}");
                }
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "dplPROVINCE_ID_change")
            {
                BoundFromCity(this.GetParamHtValue("PROVINCE_ID"));
            }
            else if (this.strAction == "dplCITY_ID_change")
            {
                BoundArea(this.GetParamHtValue("CITY_ID"));
            } 
            else if (this.strAction == "doSave")
            {
                doSave();
            }
            else if (this.strAction == "doedit")
            {
                doedit(GetParamHtValue("ID"));
            } 
        }
    }
}